<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 *
 * Created on	:  Jun 15, 2008
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RLibImporter::cls( "filter.JFilterInput" );
RCoreImporter::cls("base.BrowserInfo");
/**
 * SiteRequest Class
 *
 * This class serves to provide the Joomla Framework with a common interface to access
 * request variables.  This includes $_POST, $_GET, and naturally $_REQUEST.  Variables
 * can be passed through an input filter to avoid injection or returned raw.
 *
 * @static
 */
class SysRequest {
	/**
	 * Gets the full request path
	 *
	 * @return string
	 */
	static function getURI() {
		return $_SERVER ['REQUEST_URI'];
	}

	/**
	 * Get site domain
	 *
	 * @return string
	 */
	static function getSiteDomain() {
		return $_SERVER ['HTTP_HOST'];
	}
	
	/**
	 * Get the request resource of site.
	 *
	 * @return SiteResource
	 */
	static function &getResource() {
		static $requestRes;
		if (! empty ( $requestRes )) {
			return $requestRes;
		}
		$requestRes = new SiteResource ( );
		$requestRes->setExtend('lang', SystemSite::getLanguage());
		$requestRes->setValueFromRequest ( $_GET, true );
		$requestRes->setValueFromRequest ( $_POST, true );
		if (empty ( $requestRes->application )) {
			$config = SystemSite::getSiteConfig ();
			$requestRes->application = $config->defaultApp;
			$_REQUEST ['app'] = $config->defaultApp;
		}
		return $requestRes;
	}

	/**
	 * Get the system application
	 *
	 * @return string
	 */
	static function application() {
		$resouce = SysRequest::getResource ();
		return $resouce->application;
	}

	/**
	 * Get the request action for application
	 *
	 * @return string
	 */
	static function action() {
		$resouce = SysRequest::getResource ();
		return $resouce->action;
	}

	/**
	 * Get the request task for action
	 *
	 * @return string
	 */
	static function task() {
		$resouce = SysRequest::getResource ();
		return $resouce->task;
	}

	/**
	 * Get the request option for task
	 *
	 * @return string
	 */
	static function option() {
		$resouce = SysRequest::getResource ();
		return $resouce->option;
	}

	/**
	 * Get the Mobile infomation if exists
	 *
	 * @return MobileDetector
	 */
	static function &getMobileDetector() {
		static $mobileInfo;
		if (empty ( $mobileInfo )) {
			RCoreImporter::cls( "base.MobileDetector" );
			$mobileInfo = new MobileDetector ( );
		}
		return $mobileInfo;
	}

	/**
	 * Check if working in front end page
	 *
	 * @return boolean
	 */
	static function isMobile() {
		$config = SystemSite::getSiteConfig ();
		if ($config->mobile_supported) {
			if (SysRequest::getBool ( "isMobile" )) {
				return true;
			}
			$mobileDetector = SysRequest::getMobileDetector ();
			return $mobileDetector->isMobile ();
		}
		return false;
	}

	/**
	 * Get the browser information of the request
	 *
	 * @return BrowserInfo
	 */
	static function &getBrowserInfo() {
		static $browser;
		if (empty ( $browser )) {
			$browser = new BrowserInfo ( );
		}
		return $browser;
	}

	/**
	 * Check if requested from a Gecko browser?
	 *
	 * @return boolean
	 */
	static function isGeckoBrowser() {
		$browser = SysRequest::getBrowserInfo ();
		return $browser->isGecko;
	}

	/**
	 * Get the Ip from the client request
	 *
	 * @return string ip address
	 */
	static function getIpAddress() {
		static $ip;
		if (empty ( $ip )) {
			if (! empty ( $_SERVER ['HTTP_CLIENT_IP'] )) //check ip from share internet
			{
				$ip = $_SERVER ['HTTP_CLIENT_IP'];
			} elseif (! empty ( $_SERVER ['HTTP_X_FORWARDED_FOR'] )) //to check ip is pass from proxy
			{
				$ip = $_SERVER ['HTTP_X_FORWARDED_FOR'];
			} else {
				$ip = $_SERVER ['REMOTE_ADDR'];
			}
		}
		return $ip;
	}

	/**
	 * Check if the request is ajax or not
	 *
	 * @return boolean
	 */
	static function isAjax() {
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) || $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
			return SysRequest::getBool ( "ajax", true);
		} elseif(SystemSite::isDebug()) {
			return SysRequest::getBool ( "ajax" );
		}
		return false;
	}

	/**
	 * Check valid URL
	 *
	 * @param string $url
	 * @return boolean
	 */
	static function isValidUrl($url) {
		$url = @parse_url ( $url );

		if (! $url) {
			return false;
		}

		$url = array_map ( 'trim', $url );
		$url ['port'] = (! isset ( $url ['port'] )) ? 80 : ( int ) $url ['port'];
		$path = (isset ( $url ['path'] )) ? $url ['path'] : '';

		if ($path == '') {
			$path = '/';
		}

		$path .= (isset ( $url ['query'] )) ? "?$url[query]" : '';

		if (isset ( $url ['host'] ) and $url ['host'] != gethostbyname ( $url ['host'] )) {
			if (PHP_VERSION >= 5) {
				$headers = get_headers ( "$url[scheme]://$url[host]:$url[port]$path" );
			} else {
				$fp = fsockopen ( $url ['host'], $url ['port'], $errno, $errstr, 30 );

				if (! $fp) {
					return false;
				}
				fputs ( $fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n" );
				$headers = fread ( $fp, 128 );
				fclose ( $fp );
			}
			$headers = (is_array ( $headers )) ? implode ( "\n", $headers ) : $headers;
			return ( bool ) preg_match ( '#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers );
		}
		return false;
	}

	/**
	 * Clone site resource of system
	 *
	 * @return SiteResource
	 */
	static function cloneResource() {
		$res = SysRequest::getResource ();
		return $res->cloneObject ();
	}

	/**
	 * Fetches and returns a given variable.
	 *
	 * The default behaviour is fetching variables depending on the
	 * current request method: GET and HEAD will result in returning
	 * an entry from $_GET, POST and PUT will result in returning an
	 * entry from $_POST.
	 *
	 * You can force the source by setting the $hash parameter:
	 *
	 *   post		$_POST
	 *   get		$_GET
	 *   files		$_FILES
	 *   cookie		$_COOKIE
	 *   env		$_ENV
	 *   server		$_SERVER
	 *   method		via current $_SERVER['REQUEST_METHOD']
	 *   default	$_REQUEST
	 *
	 * @static
	 * @param	string	$name		Variable name
	 * @param	string	$default	Default value if the variable does not exist
	 * @param	string	$hash		Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
	 * @param	string	$type		Return type for the variable, for valid values see {@link JFilterInput::clean()}
	 * @return	mixed	Requested variable
	 * @since	1.5
	 */
	static function getVar($name, $default = null, $hash = 'default', $type = 'none') {
		// Ensure hash and type are uppercase
		$input = &SysRequest::_getHashInput ( $hash );

		if (array_key_exists ( $name, $input )) {
			$var = $input [$name];
		} else {
			return $default;
		}

		// do convert here
		if ($type != 'none') {
			$var = JFilterInput::clean ( $var, $type );
		}

		return $var;
	}

	/**
	 * Set value for the system array
	 *
	 * @param string $name var name
	 * @param mix $val value
	 * @param unknown_type $hash
	 * @return array set array
	 */
	static function setVar($name, $val, $hash = 'default') {
		$input = &SysRequest::_getHashInput ( $hash );
		$input [$name] = $val;
		return $input;
	}

	/**
	 * Get the input array ($_POST, $_GET, $_REQUEST,...)
	 *
	 * You can force the source by setting the $hash parameter:
	 *
	 *   post		$_POST
	 *   get		$_GET
	 *   files		$_FILES
	 *   cookie		$_COOKIE
	 *   env		$_ENV
	 *   server		$_SERVER
	 *   method		via current $_SERVER['REQUEST_METHOD']
	 *   default	$_REQUEST
	 *
	 * @param unknown_type $hash
	 * @return unknown
	 */
	static function &_getHashInput($hash = 'default') {

		// Ensure hash and type are uppercase
		$hash = strtoupper ( $hash );
		if ($hash === 'METHOD') {
			$hash = strtoupper ( $_SERVER ['REQUEST_METHOD'] );
		}
		$type = strtoupper ( $type );
		$sig = $hash . $type . $mask;

		// Get the input hash
		switch ($hash) {
			case 'GET' :
				$input = &$_GET;
				break;
			case 'POST' :
				$input = &$_POST;
				break;
			case 'FILES' :
				$input = &$_FILES;
				break;
			case 'COOKIE' :
				$input = &$_COOKIE;
				break;
			case 'ENV' :
				$input = &$_ENV;
				break;
			case 'SERVER' :
				$input = &$_SERVER;
				break;
			default :
				$input = &$_REQUEST;
				break;
		}
		return $input;
	}

	/**
	 * Fetches and returns a given filtered variable. The integer
	 * filter will allow only digits to be returned. This is currently
	 * only a proxy static function for getVar().
	 *
	 * See getVar() for more in-depth documentation on the parameters.
	 *
	 * @static
	 * @param	string	$name		Variable name
	 * @param	string	$default	Default value if the variable does not exist
	 * @param	string	$hash		Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
	 * @return	integer	Requested variable
	 */
	static function getInt($name, $default = 0, $hash = 'default') {
		return SysRequest::getVar ( $name, $default, $hash, 'int' );
	}

	/**
	 * Fetches and returns a given filtered variable.  The float
	 * filter only allows digits and periods.  This is currently
	 * only a proxy static function for getVar().
	 *
	 * See getVar() for more in-depth documentation on the parameters.
	 *
	 * @static
	 * @param	string	$name		Variable name
	 * @param	string	$default	Default value if the variable does not exist
	 * @param	string	$hash		Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
	 * @return	float	Requested variable
	 */
	static function getFloat($name, $default = 0.0, $hash = 'default') {
		return SysRequest::getVar ( $name, $default, $hash, 'float' );
	}

	/**
	 * Fetches and returns a given filtered variable. The bool
	 * filter will only return true/false bool values. This is
	 * currently only a proxy static function for getVar().
	 *
	 * See getVar() for more in-depth documentation on the parameters.
	 *
	 * @static
	 * @param	string	$name		Variable name
	 * @param	string	$default	Default value if the variable does not exist
	 * @param	string	$hash		Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
	 * @return	bool		Requested variable
	 */
	static function getBool($name, $default = false, $hash = 'default') {
		return SysRequest::getVar ( $name, $default, $hash, 'bool' );
	}

	/**
	 * Fetches and returns a given filtered variable. The word
	 * filter only allows the characters [A-Za-z_]. This is currently
	 * only a proxy static function for getVar().
	 *
	 * See getVar() for more in-depth documentation on the parameters.
	 *
	 * @static
	 * @param	string	$name		Variable name
	 * @param	string	$default	Default value if the variable does not exist
	 * @param	string	$hash		Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
	 * @return	string	Requested variable
	 */
	static function getWord($name, $default = '', $hash = 'default') {
		return SysRequest::getVar ( $name, $default, $hash, 'word' );
	}

	/**
	 * Fetches and returns a given filtered variable. The cmd
	 * filter only allows the characters [A-Za-z0-9.-_]. This is
	 * currently only a proxy static function for getVar().
	 *
	 * See getVar() for more in-depth documentation on the parameters.
	 *
	 * @static
	 * @param	string	$name		Variable name
	 * @param	string	$default	Default value if the variable does not exist
	 * @param	string	$hash		Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
	 * @return	string	Requested variable
	 */
	static function getCmd($name, $default = '', $hash = 'default') {
		return SysRequest::getVar ( $name, $default, $hash, 'cmd' );
	}

	/**
	 * Fetches and returns a given filtered variable. The string
	 * filter deletes 'bad' HTML code, if not overridden by the mask.
	 * This is currently only a proxy static function for getVar().
	 *
	 * See getVar() for more in-depth documentation on the parameters.
	 *
	 * @static
	 * @param	string	$name		Variable name
	 * @param	string	$default	Default value if the variable does not exist
	 * @param	string	$hash		Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
	 * @param	int		$mask		Filter mask for the variable
	 * @return	string	Requested variable
	 */
	static function getString($name, $default = '', $hash = 'default', $mask = 0) {
		// Cast to string, in case SysRequest_ALLOWRAW was specified for mask
		return ( string ) SysRequest::getVar ( $name, $default, $hash, 'string', $mask );
	}

	/**
	 * Gets the request method
	 *
	 * @return string
	 */
	static function getMethod() {
		$method = strtoupper ( $_SERVER ['REQUEST_METHOD'] );
		return $method;
	}

}
?>
